class Queue {
  constructor() {
    this.items = []
  }

  enqueue(element){
    this.items.push(element)
  }

  dequeue(){
    this.items.shift()
  }

  front(){
    if (this.items.length === 0) return null;
    return this.items[0]
  }

  isEmpty() {
    return this.items.length === 0;
  }

  size() {
    return this.items.length
  }
}

class QueueElement {
  constructor(element, priority) {
    this.element = element;
    this.priority = priority;
  }
}
class Priority_queue extends Queue {
  enqueue(element, priority) {
    //1. 创建QueueElement对象
    const queueElement = new QueueElement(element, priority);
    //2. 考虑插入新的元素
    if (this.isEmpty()) {
      // 如果是空的，直接插入
      this.items.push(queueElement);
    } else {
      // 不是空，要做比较
      let added = false;
      for (let i = 0; i < this.items.length; i++){
        if (this.items[i].priority < queueElement.priority){
          this.items.splice(i,0,queueElement);
          added = true;
          break;
        }
      }
      if (!added){
        //这就是元素要加在最后的情况
        this.items.push(queueElement)
      }
    }
  }
}

const priorityQueue = new Priority_queue();

// 入队 enqueue() 测试
priorityQueue.enqueue("A", 10);
priorityQueue.enqueue("B", 15);
priorityQueue.enqueue("C", 11);
priorityQueue.enqueue("D", 20);
priorityQueue.enqueue("E", 18);
console.log(priorityQueue.items);